#include <cstdio>
#include <cstdlib>
#include "Debug.hpp"

// This function must be called after GL rendering context opened.
void DebugInfo(FILE *stream){
  GLint tmp,i;
  GLubyte *str;
  //std::vector <std::string> extensions;

  fprintf(stream,"\nOpenGL Driver Info\n");
  fprintf(stream,"================\n");
  fprintf(stream,"Vendor: %s\n", glGetString(GL_VENDOR));
  fprintf(stream,"Version: %s\n", glGetString(GL_RENDERER));
  fprintf(stream,"Renderer: %s\n", glGetString(GL_VERSION));
  fprintf(stream,"GLEW version: %s\n", glewGetString(GLEW_VERSION));

  fprintf(stream,"Color Bits(R,G,B,A): (");
  glGetIntegerv(GL_RED_BITS, &tmp);
  fprintf(stream,"%d,", tmp);
  glGetIntegerv(GL_GREEN_BITS, &tmp);
  fprintf(stream,"%d,", tmp);
  glGetIntegerv(GL_BLUE_BITS, &tmp);
  fprintf(stream,"%d,", tmp);
  glGetIntegerv(GL_ALPHA_BITS, &tmp);
  fprintf(stream,"%d)\n", tmp);

  glGetIntegerv(GL_DEPTH_BITS, &tmp);
  fprintf(stream,"Depth Bits: %d\n", tmp);

  glGetIntegerv(GL_STENCIL_BITS, &tmp);
  fprintf(stream,"Stencil Bits: %d\n", tmp);

  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &tmp);
  fprintf(stream,"Max Texture Size: %dx%d\n", tmp, tmp);

  glGetIntegerv(GL_MAX_LIGHTS, &tmp);
  fprintf(stream,"Max Lights: %d\n", tmp);

  glGetIntegerv(GL_MAX_CLIP_PLANES, &tmp);
  fprintf(stream,"Max Clip Planes: %d\n", tmp);

  glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &tmp);
  fprintf(stream,"Max Modelview Matrix Stacks: %d\n", tmp);

  glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &tmp);
  fprintf(stream,"Max Projection Matrix Stacks: %d\n", tmp);

  glGetIntegerv(GL_MAX_ATTRIB_STACK_DEPTH, &tmp);
  fprintf(stream,"Max Attribute Stacks: %d\n", tmp);

  glGetIntegerv(GL_MAX_TEXTURE_STACK_DEPTH, &tmp);
  fprintf(stream,"Max Texture Stacks: %d\n", tmp);

  glGetIntegerv(GL_MAX_NAME_STACK_DEPTH, &tmp);
  fprintf(stream,"Max Name Stacks: %d\n", tmp);

  tmp = 0;
  i =0;
  str = (GLubyte*)glGetString(GL_EXTENSIONS);
  while(*(str+i) != '\0'){
    if(*(str+i) == ' '){
      tmp++;
      *(str+i) = 10; // 10 = '\n'
    }
    i++;
  }
  fprintf(stream,"Total Number of Extensions : %d\n", tmp);
  fprintf(stream,"================\n");
  fprintf(stream,"%s", str);
  fprintf(stream,"================\n");
}

